package com.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.springframework.stereotype.Service;

/**
 * @ClassName DateUtil
 * @Description 处理日期通用服务
 * @Author 李东
 * @Date 2020/06/01 10:55
 **/
@Service
public class DateUtil {

    // 获取指定月的天数
    public int getMonthLastDay(int year, int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);// 把日期设置为当月第一天
        a.roll(Calendar.DATE, -1);// 日期回滚一天，也就是最后一天
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    public Date stringToDate(String datetime) {
        SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdFormat.parse(datetime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * @description 判断时间是否在时间段内
     * @param valiDate 需要校验的时间
     * @param beginTime 范围起期
     * @param endTime 范围止期
     * @return boolean 校验通过
     */
    public boolean belongCalendar(Date valiDate, Date beginTime, Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(valiDate);
        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        if (date.after(begin) && date.before(end)) {
            return true;
        } else if (valiDate.compareTo(beginTime) == 0 || valiDate.compareTo(endTime) == 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @description 判断时间是否不超过指定日期
     * @param valiDate 需要校验的时间
     * @param endTime 指定日期
     * @return boolean 校验通过
     */
    public boolean beforeCalendar(Date valiDate, Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(valiDate);
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        if (valiDate.compareTo(endTime) <= 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取当前日期
     * 
     * @return
     */
    public static Date getNowDate() {
        Date date = new Date();
        return date;
    }

    /**
     * 字符串转date
     * 
     * @param dateString date字符串
     * @param format 格式化字符串 ：yyyyMMdd
     * @return
     */
    public static Date string2Date(String dateString, String format) {
        SimpleDateFormat sf = new SimpleDateFormat(format);
        Date dt = null;
        try {
            dt = sf.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dt;
    }


    /**
     * date转字符串
     * 
     * @param date
     * @return
     */
    public static String date2String(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(date);
    }

    /**
     * date转字符串
     * 
     * @param date
     * @return
     */
    public static String date2String(Date date, String formatStr) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(formatStr);
        return dateFormat.format(date);
    }



    /**
     * 指定日期之前或之后mon个月的日期
     * 
     * @param date
     * @param mon
     * @return
     */
    public static Date dateByMonth(Date date, Integer mon) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, mon);
        Date dateMon = c.getTime();
        return dateMon;
    }

    /**
     * 获取指定日期的月份对应的第一天的00:00:00
     * 
     * @param date
     * @return
     */
    public static Date firstDate(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, 0);
        // 设置为1号,当前日期既为本月第一天
        c.set(Calendar.DAY_OF_MONTH, 1);
        // 将小时至0
        c.set(Calendar.HOUR_OF_DAY, 0);
        // 将分钟至0
        c.set(Calendar.MINUTE, 0);
        // 将秒至0
        c.set(Calendar.SECOND, 0);
        // 将毫秒至0
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 取指定日期的月份对应的最后一天的23:59:59
     * 
     * @param date
     * @return
     */
    public static Date lastDate(Date date) {
        // 获取当前月最后一天
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        // 将小时至0
        c.set(Calendar.HOUR_OF_DAY, 23);
        // 将分钟至0
        c.set(Calendar.MINUTE, 59);
        // 将秒至0
        c.set(Calendar.SECOND, 59);
        // 将毫秒至0
        c.set(Calendar.MILLISECOND, 999);
        return c.getTime();
    }


}
